﻿Public Class Form2
    Dim da As New Class1
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim strSql As String = ""

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Form2_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Form1.Enabled = True
        strSql = "select * from video order by video_id"
        ds = da.PopulateList(strSql)
        dt = ds.Tables(0)
        Form1.DataGridView1.DataSource = dt
        Form1.DataGridView1.AllowUserToResizeRows = False
        For Each column As DataGridViewColumn In Form1.DataGridView1.Columns
            Select Case column.HeaderText
                Case "video_id"
                    column.HeaderText = "เลขทะเบียน"
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                Case "video_title"
                    column.HeaderText = "ชื่อเรื่อง"
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                Case "video_type"
                    column.HeaderText = "ประเภท"
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                Case "video_status"
                    column.HeaderText = "สถาณะ"
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            End Select
        Next
        Form1.DataGridView1.Show()
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = New Point(Form1.Location.X + Form1.Width / 3.65, Form1.Location.Y + Form1.Height / 5)
        Form1.Enabled = False
        If Form1.x = False Then
            TextBox1.Text = Form1.DataGridView1.SelectedRows(0).Cells(1).Value
            TextBox2.Text = Form1.DataGridView1.SelectedRows(0).Cells(2).Value
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If String.IsNullOrEmpty(TextBox1.Text) = True Then
            MsgBox("Please Enter Video Name")
            Exit Sub
        End If
        If String.IsNullOrEmpty(TextBox2.Text) = True Then
            MsgBox("Please Enter Video Type")
            Exit Sub
        End If
        If MsgBox("คุณต้องการบันทึกใช่หรือไม่", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            If Form1.x = False Then
                strSql = "update video set video_title='" & TextBox1.Text & "',video_type='" & TextBox2.Text & "' where video_id='" & Form1.DataGridView1.SelectedRows(0).Cells(0).Value & "' "
                da.PopulateList(strSql)
                MsgBox("Finished Edit " & TextBox1.Text & " Data")
                Me.Close()
            Else
                strSql = "insert into video(video_title,video_type,video_status) values('" & TextBox1.Text & "','" & TextBox2.Text & "','Available') "
                da.PopulateList(strSql)
                MsgBox("Finished Add New Data")
                Me.Close()
            End If
        End If
    End Sub
End Class